package com.jrx.anytxn.investor.job.file.day.order.config;

import com.jrx.anytxn.investor.config.AnytxnBatchProperties;
import com.jrx.anytxn.investor.config.InvestorFileConfig;
import com.jrx.anytxn.investor.constant.TransBizConstant;
import com.jrx.anytxn.investor.entity.AmLoanAccountInvestorInfo;
import com.jrx.anytxn.investor.job.file.day.order.step.*;
import com.jrx.anytxn.investor.service.IInvestorFileService;
import com.jrx.anytxn.param.dto.investor.FileBean;
import com.jrx.anytxn.param.entity.PrInvestorTable;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrInvestorTableMapper;
import com.jrx.anytxn.param.service.investor.IInvestorParamFileService;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: 生成日初借据信息文件
 * @description:
 * @author: xiaoyu.jing
 * @date: 2020-06-08 16:40
 **/
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class InvestorDayOrderConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    private SqlSessionFactory sqlSessionFactory;
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private DayOrderProcessor dayOrderProcessor;
    @Autowired
    private IInvestorParamFileService fileService;
    @Autowired
    private InvestorFileConfig fileConfig;
    @Resource
    private ExtPrInvestorTableMapper extPrInvestorTableMapper;

    @Bean
    public Job dayOrderJob() {
        return jobBuilderFactory.get("dayOrderJob")
                .start(dayOrderFirstStep())
                .next(dayOrderSecondStep())
                .listener(dayOrderListener())
                .build();
    }

    @Bean
    public Step dayOrderFirstStep() {
        return stepBuilderFactory.get("dayOrderFirstStep")
                .<AmLoanAccountInvestorInfo, FileBean>chunk(anytxnBatchProperties.getFileConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(dayOrderFirstReader())
                .processor(dayOrderProcessor)
                .writer(dayOrderWriter())
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getFileConfig().getThrottleLimit())//多线程
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<AmLoanAccountInvestorInfo> dayOrderFirstReader() {
        return new DayOrderFirstReader(sqlSessionFactory, anytxnBatchProperties.getFileConfig().getPageSize());
    }

    @Bean
    public Step dayOrderSecondStep() {
        return stepBuilderFactory.get("dayOrderSecondStep")
                .<AmLoanAccountInvestorInfo, FileBean>chunk(anytxnBatchProperties.getFileConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(dayOrderSecondReader())
                .processor(dayOrderProcessor)
                .writer(dayOrderWriter())
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getFileConfig().getThrottleLimit())//多线程
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<AmLoanAccountInvestorInfo> dayOrderSecondReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        return new DayOrderSecondReader(sqlSessionFactory, anytxnBatchProperties.getFileConfig().getPageSize(),org);
    }

    @Bean
    @StepScope
    public ItemWriter<FileBean> dayOrderWriter() {
        return new FileWriter(fileService, fileConfig,TransBizConstant.DAY_ORDER_FILE_NAME);
    }

    @Bean
    public JobExecutionListener dayOrderListener(){
        List<PrInvestorTable> investorList = extPrInvestorTableMapper.selectAllByTableStatus(TransBizConstant.STATUS_1);
        return new FileListener(organizationTableService,fileConfig, investorList,TransBizConstant.DAY_ORDER_FILE_NAME);
    }
}
